package base;

import freemarker.template.TemplateException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfig;
import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver;

import javax.annotation.PostConstruct;

/**
 * Created by zhou on 2017/5/5.
 */
//@Configuration
public class FreemarkerConfig {

    private Logger logger = LoggerFactory.getLogger(FreeMarkerConfig.class);


    @Autowired
    protected freemarker.template.Configuration freeMarkerConfiguration;

    @Autowired
    protected FreeMarkerViewResolver resolver;

    @Autowired
    protected InternalResourceViewResolver springResolver;


    @PostConstruct
    public void setSharedVariable(){

        freeMarkerConfiguration.setDateFormat("yyyy/MM/dd");
        freeMarkerConfiguration.setDateTimeFormat("yyyy-MM-dd HH:mm:ss");

        //下面三句配置的就是我自己的freemarker的自定义标签，在这里把标签注入到共享变量中去就可以在模板中直接调用了
        //configuration.setSharedVariable("content_list", new ContentListDirective());
        //configuration.setSharedVariable("article_list", new ArticleDirective());
        //configuration.setSharedVariable("channel_list", new ChannelListDirective());


        /*Setting 配置*/

        try {
            freeMarkerConfiguration.setSetting("template_update_delay","1");
            freeMarkerConfiguration.setSetting("default_encoding","UTF-8");
        } catch (TemplateException e) {
            logger.error("freemarker配置类出错");
            throw new RuntimeException(e);
        }

        /*配置spring的视图解析器*/
        springResolver.setPrefix("/templates/");
        springResolver.setSuffix(".jsp");


        /*配置freemarker的视图解析器*/
        resolver.setSuffix(".ftl");
        resolver.setCache(false);
        resolver.setRequestContextAttribute("request");
        resolver.setOrder(0);




    }

}
